package components
{
	import UIFree.uif_loader;
	
	import data.cd_victoryTmpl;
	import data.clt_data;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.geom.Matrix;

	public class clt_victoryCollage extends Object {
		public static function create(victoryTmpl: uint, collageID: uint, posMatrix: Matrix, resFunc: Function): void {
			function doComplete(): void {
				var result: Bitmap = new Bitmap(new BitmapData(mask.width, mask.height, false, 0xFFFFFF));
				var scale: Number = mask.width / collage.width;
				var mat: Matrix = new Matrix();
				mat.translate(-collage.width / 2, -collage.height / 2);
				mat.scale(scale, scale);
				mat.concat(posMatrix);
				mat.translate(collage.width / 2 * scale, collage.height / 2 * scale);
				result.bitmapData.draw(collage, mat, null, null, null, true);
				result.bitmapData.draw(mask);
				resFunc(result);				
			}
			
			var mask: uif_loader = new uif_loader(null, clt_servers.IMAGE_URL + 'victory/' + victoryTmpl + clt_data.PNGEXT, function(e: Event): void {
				if (mask.loaded && collage.loaded) doComplete();
			});
			
			var collage: uif_loader = new uif_loader(null, clt_servers.GAME_URL + collageID + clt_data.JPGEXT, function(e: Event): void {
				if (mask.loaded && collage.loaded) doComplete();
			});
		}
		
		public static function createFromTmpl(tmplIndex: int, collageID: uint, resFunc: Function): void {
			var am: Array = cd_victoryTmpl.list[tmplIndex].matrix;
			clt_victoryCollage.create(cd_victoryTmpl.list[tmplIndex].id, collageID, 
								new Matrix(am[0], am[1], am[2], am[3], am[4], am[5]), resFunc);
		}
	}
}